<?php

namespace App\Services;

use Mockery\Exception;

/**
 *
 * Class RefundCate
 * @package App\Services
 */
class RefundCate extends BaseService
{


    public $RefundModel;

    /**
     * RefundCate constructor.
     */
    public function __construct()
    {
        $this->RefundModel = new \App\Models\RefundCate();
    }

    /**
     * 2021/3/12--10:12
     * @author:董宗澳
     * @email:2453611300
     * @wechat:DZA74941
     * @QQ:2453611300
     * @web:www.quzha.net
     * @method:getList
     * @effect: 售后问题列表
     */
    public function getList($limit = 30, $if_paginate = true, $if_code_return = true)
    {
        $where = [];
        if (request()->is_show) {
            $where['is_show'] = 0;
        }
        $object = $this->RefundModel->where($where);
        if (\request()->keywords) {
            $object->where('name', \request()->keywords);
        }
        if ($if_paginate) {
            $list = $object->orderBy('sort', 'desc')->paginate($limit)->toArray();
        } else {
            $list = $object->orderBy('sort', 'desc')->select()->get()->toArray();
        }
        if ($if_code_return) {
            return $this->success($list);
        } else {
            return $this->format($list);
        }
    }

    /**
     * 2021/3/12--15:03
     * @return array
     * @author:董宗澳
     * @email:2453611300
     * @wechat:DZA74941
     * @QQ:2453611300
     * @web:www.quzha.net
     * @method:add
     * @effect:
     */
    public function add()
    {
        try {
            $data = [
                'name' => request()->name,
                'is_show' => request()->is_show
            ];
            if ($this->RefundModel->where('name', $data['name'])->first()) {
                return $this->error('请勿重复添加分类');
            }
            $this->RefundModel->create($data);
            return $this->success([], '执行成功');
        } catch (\Exception $e) {
            return $this->error('执行失败' . $e->getMessage());
        }
    }


    public function edit($id)
    {
        try {
            $data = [
                'name' => request()->name,
                'sort' => request()->sort,
            ];
            $this->RefundModel->where('id', $id)->update($data);
            return $this->success([], '修改成功');
        } catch (\Exception $e) {
            return $this->error([], '执行失败' . $e->getMessage());
        }
    }

    /**
     * @return array 数组
     */
    public function show($id)
    {
        try {
            $data = $this->RefundModel->where('id', $id)->first()->toArray();
            return $this->success($data, '执行成功');
        } catch (\Exception $e) {
            return $this->error([], '执行失败' . $e->getMessage());
        }
    }


    /**
     *删除
     */
    public function del($ids)
    {
        return $this->deletes($ids) ? $this->success('删除成功!') : $this->error('删除失败!');
    }

    // 删除
    public function deletes($ids)
    {
        $ids = explode(',', $ids);
        try {
            foreach ($ids as $key => $val) {
                $this->RefundModel->where('id', $val)->delete();
            }
            return true;
        } catch (Exception $exception) {
            return false;
        }
    }

    /**
     *
     */
    public function __destruct()
    {
        $this->RefundModel;
        // TODO: Implement __destruct() method.
    }
}
